package com.rocket.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.rocket.entity.RocketRYI6300;

public class RocketRYI6300Sql {

	private static RocketRYI6300Sql	instance						= null;
	private final String			TABLE_NAME						= "RocketRYI6300";

	private final String			KEY_ROCKETRYI6300_ID			= "id";
	private final String			KEY_ROCKETRYI6300_ANGEL			= "angel";			//角度
	private final String			KEY_ROCKETRYI6300_START_LENGTH	= "startlength";
	private final String			KEY_ROCKETRYI6300_START_HEIGHT	= "startheight";
	private final String			KEY_ROCKETRYI6300_END_LENGTH	= "endlength";
	private final String			KEY_ROCKETRYI6300_END_HEIGHT	= "endheight";
	private final String			KEY_ROCKETRYI6300_TOP_LENGTH	= "toplength";
	private final String			KEY_ROCKETRYI6300_TOP_HEIGHT	= "topheight";
	private final String			KEY_ROCKETRYI6300_LENGTH		= "length";

	public static synchronized RocketRYI6300Sql getInstance() {
		if (null == instance) {
			instance = new RocketRYI6300Sql();
		}
		return instance;
	}

	public void init() {
		createTable();
		initData();
	}

	private void createTable() {
		if (DataBaseOpenHelper.mDataBase != null) {
			String sql = "drop table if exists " + TABLE_NAME;
			DataBaseOpenHelper.mDataBase.execSQL(sql);
			sql = "create table if not exists " + TABLE_NAME + "( " + KEY_ROCKETRYI6300_ID + " INTEGER PRIMARY KEY," + KEY_ROCKETRYI6300_ANGEL + " INTEGER,"
					+ KEY_ROCKETRYI6300_START_LENGTH + " INTEGER," + KEY_ROCKETRYI6300_START_HEIGHT + " INTEGER,"
					+ KEY_ROCKETRYI6300_END_LENGTH + " INTEGER," + KEY_ROCKETRYI6300_END_HEIGHT + " INTEGER,"
					+ KEY_ROCKETRYI6300_TOP_HEIGHT + " INTEGER," + KEY_ROCKETRYI6300_TOP_LENGTH + " INTEGER,"
					+ KEY_ROCKETRYI6300_LENGTH + " INTEGER)";
			DataBaseOpenHelper.mDataBase.execSQL(sql);
		}
	}

	public long insert(RocketRYI6300 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETRYI6300_ANGEL, rocket.angle);
			values.put(KEY_ROCKETRYI6300_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETRYI6300_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETRYI6300_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETRYI6300_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETRYI6300_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETRYI6300_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETRYI6300_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.insert(TABLE_NAME, null, values);
		}
		return 0;
	}

	public long update(RocketRYI6300 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETRYI6300_ANGEL, rocket.angle);
			values.put(KEY_ROCKETRYI6300_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETRYI6300_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETRYI6300_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETRYI6300_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETRYI6300_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETRYI6300_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETRYI6300_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.update(TABLE_NAME, values, KEY_ROCKETRYI6300_ID + " = ?", new String[] { String.valueOf(rocket.id) });
		}
		return 0;
	}

	public void delete(RocketRYI6300 rocket) {
		delete(rocket.id);
	}

	public void delete(int id) {
		if (DataBaseOpenHelper.mDataBase != null) {
			DataBaseOpenHelper.mDataBase.delete(TABLE_NAME, KEY_ROCKETRYI6300_ID + " = ?", new String[] { String.valueOf(id) });
		}
	}

	public ArrayList<RocketRYI6300> query() {
		ArrayList<RocketRYI6300> rockets = new ArrayList<RocketRYI6300>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, null, null, null, null, KEY_ROCKETRYI6300_TOP_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						RocketRYI6300 rocket = new RocketRYI6300();
						rocket.id = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_ID));
						rocket.angle = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_ANGEL));
						rocket.startLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_START_LENGTH));
						rocket.startHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_START_HEIGHT));
						rocket.endLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_END_LENGTH));
						rocket.endHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_END_HEIGHT));
						rocket.topLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_TOP_LENGTH));
						rocket.topHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_TOP_HEIGHT));
						rocket.length = cur.getInt(cur.getColumnIndex(KEY_ROCKETRYI6300_LENGTH));
						rockets.add(rocket);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return rockets;
	}

	private void initData() {
		RocketRYI6300 rocket = new RocketRYI6300();
		
		rocket.angle = 85;
		rocket.startLength = 563;
		rocket.startHeight = 5542;
		rocket.topLength = 913;
		rocket.topHeight = 6837;
		rocket.endLength = 999;
		rocket.endHeight = 6750;
		rocket.length = 1639;
		insert(rocket);
		
		rocket.angle = 80;
		rocket.startLength = 1121;
		rocket.startHeight = 5456;
		rocket.topLength = 1787;
		rocket.topHeight = 6688;
		rocket.endLength = 1977;
		rocket.endHeight = 6582;
		rocket.length = 3138;
		insert(rocket);
		
		rocket.angle = 75;
		rocket.startLength = 1666;
		rocket.startHeight = 5318;
		rocket.topLength = 2607;
		rocket.topHeight = 6444;
		rocket.endLength = 2918;
		rocket.endHeight = 6306;
		rocket.length = 4578;
		insert(rocket);
		
		rocket.angle =70;
		rocket.startLength = 2192;
		rocket.startHeight = 5123;
		rocket.topLength = 3347;
		rocket.topHeight = 6121;
		rocket.endLength = 3804;
		rocket.endHeight = 5898;
		rocket.length = 5793;
		insert(rocket);
		
		rocket.angle = 65;
		rocket.startLength = 2693;
		rocket.startHeight = 4881;
		rocket.topLength = 3984;
		rocket.topHeight = 5727;
		rocket.endLength = 4632;
		rocket.endHeight = 5742;
		rocket.length = 6813;
		insert(rocket);
		
		rocket.angle = 60;
		rocket.startLength = 3164;
		rocket.startHeight = 4591;
		rocket.topLength = 4528;
		rocket.topHeight = 5279;
		rocket.endLength = 5381;
		rocket.endHeight = 4935;
		rocket.length = 7639;
		insert(rocket);
		
		rocket.angle = 55;
		rocket.startLength = 3602;
		rocket.startHeight = 4259;
		rocket.topLength = 4951;
		rocket.topHeight = 4788;
		rocket.endLength = 6049;
		rocket.endHeight = 4331;
		rocket.length = 8272;
		insert(rocket);
		
		rocket.angle = 50;
		rocket.startLength = 4000;
		rocket.startHeight = 3887;
		rocket.topLength = 5273;
		rocket.topHeight = 4270;
		rocket.endLength = 6628;
		rocket.endHeight = 3679;
		rocket.length = 8719;
		insert(rocket);
		
		rocket.angle = 45;
		rocket.startLength = 4385;
		rocket.startHeight = 3484;
		rocket.topLength = 5463;
		rocket.topHeight = 3734;
		rocket.endLength = 7116;
		rocket.endHeight = 2984;
		rocket.length = 8989;
		insert(rocket);

	}
}
